package com.kzd.gtboon.main.game

import android.view.LayoutInflater
import androidx.lifecycle.LifecycleOwner
import androidx.recyclerview.widget.LinearLayoutManager
import com.kzd.gtboon.R
import com.kzd.gtboon.custom.setTransitionStatusE
import com.kzd.gtboon.databinding.ActivityServiceMoreBinding
import com.kzd.gtboon.jetpack.vm.GameViewModel
import com.kzd.gtboon.main.KEY_GAME_DETAILS_ID
import com.kzd.gtboon.main.KEY_GAME_DETAILS_TITLE
import com.kzd.gtboon.main.game.adapter.GameServiceAdapter
import com.module.lemlin.app.OwnerStatusBar
import com.module.lemlin.http.Status
import com.module.lemlin.owner.OwnerViewBindingActivity
import com.module.lemlin.owner.ownerViewModel

class ServiceMoreActivity : OwnerViewBindingActivity<ActivityServiceMoreBinding>() {

    private val mGameServiceAdapter = GameServiceAdapter()

    private val mTitle: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_TITLE) ?: ""

    private val mGameId: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_ID) ?: ""

    private val mViewModel: GameViewModel by lazy {
        ownerViewModel(owner = this)
    }

    override val inflate: (LayoutInflater) -> ActivityServiceMoreBinding
        get() = ActivityServiceMoreBinding::inflate

    override fun initStatusBar(statusBar: OwnerStatusBar) {
        statusBar.run {
            addView = mViewBinding.flServiceMoreBar
            barColor = R.color.white
        }
        super.initStatusBar(statusBar)
    }

    override fun initView() {

        mViewBinding.flServiceMoreBar.setTitleTextContent(mTitle)

        mViewBinding.includeServiceMoreBody.rvAppRefreshBody.apply {
            layoutManager = LinearLayoutManager(context)
            itemAnimator = null
        }.adapter = mGameServiceAdapter

        mGameServiceAdapter.setOnItemChildClickListener { _, view, position ->
            val itemData = mGameServiceAdapter.getItem(position)
            if (view.id == R.id.tvGameServiceRemindStatus) {
                mViewModel.gameServiceRemind(itemData.id, itemData.remind_rs, position)
            }
        }

        mViewBinding.includeServiceMoreBody.mSmartRefreshLayout.setOnRefreshListener {
            mViewModel.gameService(mGameId, limit = 20)
        }
        mViewBinding.includeServiceMoreBody.mSmartRefreshLayout.setOnLoadMoreListener {
            mViewModel.gameService(mGameId, isLoadMore = true)
        }
    }

    override fun initData() {
        mViewModel.gameService(mGameId, limit = 20)
    }

    override fun initViewMode() {
        mViewModel.gameServiceLiveData.observe(this as LifecycleOwner, { response ->
            setTransitionStatusE(response, mGameServiceAdapter, mViewBinding.includeServiceMoreBody.mSmartRefreshLayout)
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val isLoadMore = response.data?.isLoadMore
                ?: return@observe
            if (isLoadMore) {
                mGameServiceAdapter.addData(data)
            } else {
                mGameServiceAdapter.setNewInstance(data)
            }
        })

        mViewModel.serviceRemindLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS) {
                return@observe
            }
            val data = response.data
                ?: return@observe
            val gameService = mGameServiceAdapter.data[data.position].apply {
                this.remind_rs = data.isRemind
            }
            mGameServiceAdapter.setData(data.position, gameService)
        })
    }
}